home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / CGIshell 1.3.2 / Pocket 6.5 / Extensions / Strings.pf < prev   
Text File  |  1995-11-11  |  5KB  |  129 lines

  1. ( Strings 10/15/95 23:30:19 )
  2. \ These words deal with 0 terminated strings as in Ron Kneusel's CGIShell.
  3. \  <ftp://kreeft.intmed.mcw.edu/Q/pub/forth/cgishell.sit.hqx>
  4. \
  5. \ Several words in this set are borrowed from rtk's CGIShell, some renamed,
  6. \ some modified. The names maintain compatability with the word-set in
  7. \  _Library of Forth Routines and Utilities_  by  James D. Terry
  8. \  (c) 1986 Shadow Lawn Press  ISBN 0-452-25841-3
  9. \
  10. \ In comments, string is the starting address of a zero terminated string,
  11. \ and len is the length not including the zero. String[255] is a length
  12. \ byte preceded string, with a max length of 255 bytes.
  13. \
  14. \ String format:
  15. \ string address is first byte ->This is a string.0<- Ends with a zero
  16.  
  17. \ Length and $clear get used a lot - do them in ml.
  18. : LENGTH ( string -- len )  \ length of the string at addr
  19.  ( was:  dup >r BEGIN dup c@ WHILE 1+ REPEAT  r> - ; )
  20.     ,$ 3016          \     move (ps),d0
  21.     ,$ 4a33 ,$ 0000  \ @0: tst.b 0(bp,d0.w)
  22.     ,$ 6706          \     beq.s @1
  23.     ,$ 0640 ,$ 0001  \     addi #1,d0
  24.     ,$ 60f4          \     bra.s @0
  25.     ,$ 9056          \ @1: sub (ps),d0
  26.     ,$ 3c80 ;        \     move d0,(ps)
  27.  
  28. : $CLEAR  ( string -- ) \ erase a string ( equivalent to: 0 swap c! ; )
  29.    ,$ 301E  ,$ 4233 ,$ 0000 ;  \ move (ps)+,d0  clr.b 0(bp,d0.w)
  30.  
  31. \ The next 4 words are directly from Ron's CGI Framework.
  32.  
  33. \ Convert between null terminated and length byte preceeded type strings.
  34. : >NULL ( string[255] -- )  \ convert a string[255] into a string
  35.     dup c@ 2dup + >r swap dup 1+ swap rot cmove  r> $clear ;
  36.  
  37. : >COUNT ( string -- ) \ convert a string into a string[255]
  38.     dup length >r dup dup 1+ r cmove  r> swap c! ;  
  39.  
  40.  
  41. \ Terminal I/O.
  42. : 0TYPE ( string -- )  \ type null terminated string
  43.     dup length dup IF type ELSE 2drop THEN ;
  44.     
  45. : ACCEPT ( string len -- )  \ like expect but stores zero at end of line
  46.     2dup 1+ 0 fill  >r dup r> expect dup length 1- + $clear ; ( bug fixed)
  47.  
  48.  
  49. \ Test a string's content.
  50. : $= ( string1 string2 -- f ) \ true if string2,len2 = string1,len1
  51.     dup length 1+  -1 swap 2swap rot 0 DO  \ set flag to true
  52.       over r + c@  over r + c@  =         \  check each byte
  53.       0= IF rot 1+ rot rot leave THEN    \   change flag to false
  54.     LOOP 2drop ;
  55.  
  56.  
  57. \ Manipulate strings.
  58. : $COPY ( source.string dest.string -- ) \ copy source to dest
  59.     over length 1+ cmove ;
  60.  
  61. : $+ ( source.string dest.string -- ) \ append source to the end of dest
  62.     dup length + $copy ;
  63.  
  64. : $LEFT ( string len -- ) \ clip string to len chars
  65.     over length min  +  $clear ;
  66.  
  67. : $RIGHT ( string len -- ) \ clip string to rightmost len characters
  68.     over length over - 0> IF
  69.       over length over -  rot dup rot +  swap rot 1+  cmove
  70.     ELSE 2drop THEN ;
  71.  
  72. : $MID ( string start len -- ) \ clip string to len section at start
  73.     rot rot over length  swap - 1+  >r dup r> $right  swap $left ;
  74.  
  75. : $UPPER ( string -- ) dup >count  dup upper  dup >null drop ; \ uppercase
  76.  
  77.  
  78. \ Find and replace with strings.
  79. variable POS  ( local variable )
  80. : $FIND ( string1 string2 -- pos ) \ find string2 in string1; 0 if unfound
  81.     0 pos !
  82.     over length over length - 2+  1  DO
  83.       over here $copy
  84.       here  over length  r swap  $mid
  85.       here over
  86.       $= IF  r pos !  leave THEN
  87.     LOOP  2drop
  88.     pos @ ;
  89.  
  90. : $REPLACE ( dest.string1 find.string2 replace.string3 -- )
  91.     rot >r swap
  92.     r over $find ?dup IF  \   IF string2 is found in string1
  93.       r here $copy         \  THEN replace string2 with string3
  94.       r over 1-  $left      \  modify string1
  95.       rot r $+
  96.       swap length +           \        !!! IMPORTANT !!!
  97.       here length  swap - 1+   \   DOES NOT CHECK FOR OVERWRITE
  98.       here swap $right          \  String1 MUST accomodate any
  99.       here r> $+                 \ additional bytes from string3
  100.     ELSE 2drop r> drop  THEN ;
  101.  
  102. \ Create and assign strings of several varieties.
  103. : $CONSTANT  \ compiling: ( -- ) name a string terminated with '}'
  104.     CREATE  125 word here c@ 1+ dup 2 mod + allot  0 [compile] ,
  105.     DOES>  count drop ;  \ runtime action: ( -- string )
  106. \ This uses a curley brace because they aren't used much on web pages.
  107. \   eg:  $constant ESERROR Empty stack!}
  108.  
  109. : $VARIABLE CREATE 1+ allot ;  \ compiling: ( len -- ) name an empty string
  110. \   eg:  80 $variable INPUTLINE  inputline ${ Hi there!}
  111.  
  112. : $ARRAY \ create named string arrays - name from input stream
  113.     CREATE  dup ,  * allot    \ compiling: ( number_of_.strings len -- )
  114.     DOES>  dup @ rot * + 2+ ;  \ runtime: ( string_number -- string )
  115. \   eg:  15 64 $array ERRORMESSAGES
  116. \        0 errorMessages ${ Error!}
  117.  
  118. \ NOTE: Constants and variables are identical except that constants
  119. \       have no room to grow, but variables _may_ have extra memory
  120. \       allotted to them to grow into.  Also constants are assigned
  121. \       when they are created, while variables (and arrays, which are
  122. \       lists of variables) must be assigned seperately (see below).
  123.  
  124. : ${ ( string -- ) \ assign text to a string from the input stream.
  125.     125 word  here >null  here swap $copy ;
  126. \   eg:  inputLine ${ Something to say!}    *** NO OVERWRITE CHECK ***
  127.  
  128.